-- DrunkBox
-- Created by chengb Jul/29/2016
-- 醉汉储物箱格子

-- 初始化
function initDrunkBox(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            local function okFunc()
                if not BraveVSDragonM.isDrunkDisapear() then
                    -- 醉汉挡住去路
                    alert(getLocStr("drunk_blocking"));
                    return;
                end

                DungeonActionM.go("open_drunk_box", self.gridData:getPos());
            end

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 如果已经领取过奖励，则格子隐藏
    local isVisible = not BraveVSDragonM.hasGotBuildingBonus(self.gridData.class);
    self.icon:setVisible(isVisible);
end
